package hn.cch.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue;


/**
 * 模拟调度
 */
public class Execute {

    private static Logger logger = LoggerFactory.getLogger(Execute.class);

    public static void main(String[] args) throws InterruptedException {

        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(3);
        arrayBlockingQueue.put("111");
        arrayBlockingQueue.put("222222");
        arrayBlockingQueue.put("333333333");

        HashMap<String, Station> stringStationHashMap = new HashMap<>();
        stringStationHashMap.put("111", new Station("111"));
        stringStationHashMap.put("222222", new Station("222222"));
        stringStationHashMap.put("333333333", new Station("333333333"));


        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        try {
                            String string = arrayBlockingQueue.take();
                            Station station = stringStationHashMap.get(string);
                            int random = (int) (Math.random() * 10);
                            if (random < 7) {//查询操作占70%
                                station.selectHandle();
                            } else {//更新操作占30%
                                station.updateHandle();
                            }
                            arrayBlockingQueue.put(string);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            }, "thread-" + i).start();
        }
    }

}

